package com.kelvin.paopao.module.chat.message;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.MessageAdapter;
import com.kelvin.paopao.adapter.MessageMultiAdapter;
import com.kelvin.paopao.api.responseBean.KefuModel;
import com.kelvin.paopao.api.responseBean.MultipleItem;
import com.kelvin.paopao.injector.components.DaggerMessageComponent;
import com.kelvin.paopao.injector.modules.MessageModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.chat.chat.ChatActivity;
import com.kelvin.paopao.module.chat.kefu.KefuActivity;
import com.kelvin.paopao.utils.AnimateHelper;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by MCK on 2017/5/9.
 */

public class MessageActivity extends BaseActivity<IMessagePresenter> implements IMessageView {

    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    @Inject
    MessageMultiAdapter messageAdapter;

    private LinearLayoutManager manager;

    private boolean isEdit;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_message;
    }

    @Override
    protected void initInjector() {
        DaggerMessageComponent.builder().applicationComponent(getAppComponent()).
                messageModule(new MessageModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        customToolbar.setTitle("消息");
        customToolbar.inflateMenu(R.menu.menu_edit_message);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                isEdit = !isEdit;
                if (isEdit) {
                    messageAdapter.setDelete(true);
                } else {
                    messageAdapter.setDelete(false);
                }
                return true;
            }
        });

        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        messageAdapter.setOnItemChildClickListener(
                new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        if (view.getId() == R.id.rl_delete) {
                            //移除conversation
                            deleteConversation(adapter.getData(), true, position);
                            mPresenter.loadMessage();
                        }
                    }
                });
        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (messageAdapter.getItem(position).getData() instanceof EMConversation) {
                    ChatActivity.launch(MessageActivity.this, ((EMConversation)
                            messageAdapter.getItem(position).getData()).conversationId());
                } else {
                    KefuActivity.launch(MessageActivity.this, kefuList);
                }
            }
        });

        recyclerView.setAdapter(messageAdapter);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
//        mPresenter.loadMessage();
    }

    private List<MultipleItem> chatList;
    private List<KefuModel> kefuList;

    @Override
    public void loadData(List<MultipleItem> conversationList) {
        if (conversationList == null || conversationList.size() == 0) {
            View noDataView = LayoutInflater.from(this).inflate(R.layout.view_no_data,
                    (ViewGroup) recyclerView.getParent(), false);
            messageAdapter.setEmptyView(noDataView);
        }
        chatList = new ArrayList<>();
        kefuList = new ArrayList<>();
        for (MultipleItem item : conversationList) {
            if (item.getItemType() == MultipleItem.COMMON) {
                chatList.add(item);
            } else if (item.getItemType() == MultipleItem.KEFU) {
                kefuList.add((KefuModel) item.getData());
            }
        }
        MultipleItem multipleItem = new MultipleItem(MultipleItem.KEFU_COLLECTION, null);
        chatList.add(0, multipleItem);
        messageAdapter.setNewData(chatList);
    }

    /**
     * 删除对话
     *
     * @param deleteMessage
     * @param position
     */
    private void deleteConversation(List<EMConversation> sortedConversatoin,
                                    boolean deleteMessage, int position) {
        EMConversation conversation = sortedConversatoin.get(position);
        EMConversation tobeDeleteCons = null;

        tobeDeleteCons = conversation;
        try {
            // 删除响应的对话
            EMClient.getInstance().chatManager().deleteConversation(tobeDeleteCons.conversationId()
                    , deleteMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, MessageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.loadMessage();
    }
}
